import { Component } from "@angular/core";
import { NavController, NavParams, AlertController } from "ionic-angular";
import { HttpService } from "../../../providers/http.service";
import { CommonFunc } from "../../../providers/commonFunc";
import { Storage } from "@ionic/storage";
import { SafeControlPage } from "../safe-control/safe-control";
import * as moment from "moment";
/**
 * 单位安全管理检查
 */
@Component({
  templateUrl: "record-check.html"
})
export class RecordCheckPage {
  check = "0";
  list: any = [];
  resultSS = [];
  checkTimeFrom: any;
  constructor(
    public navCtrl: NavController,
    private alertCtrl: AlertController,
    private httpService: HttpService,
    private navParams: NavParams,
    private commonFunc: CommonFunc,
    private storage: Storage
  ) {
    this.checkTimeFrom = moment(new Date()).format("YYYYMMDDHHmmss");
    if (this.navParams.get("id")) {
      this.httpService
        .queryPlanDictItem1({ pDictId: this.navParams.get("id") })
        .subscribe(data => {
          if (data.status == "200") {
            this.list = data.data.list;
            this.list.forEach(element => {
              element.itemContents.forEach(element2 => {
                this.resultSS.push({
                  pditemid: element2.id,
                  result: null,
                  content: element2.itemContent
                });
              });
            });
          }
        });
    }
  }

  submit() {
    for (let i = 0; i < this.resultSS.length; i++) {
      if (!this.resultSS[i].result) {
        const alert = this.alertCtrl.create({
          subTitle: "检查项填写不完整",
          buttons: ["确定"]
        });
        alert.present();
        return;
      }
    }

    this.navCtrl.push(SafeControlPage, {
      checkRes: this.resultSS,
      pdctId: this.navParams.get("id"),
      pdId: this.navParams.get("pdId"),
      beCkedPartyName: this.navParams.get("beCkedPartyName"),
      beCkedPartyId: this.navParams.get("beCkedPartyId"),
      pdType: this.navParams.get("pdType"),
      checkTimeFrom: this.checkTimeFrom,
    });
  }

  changSel(id, result) {
    for (let i = 0; i < this.resultSS.length; i++) {
      if (this.resultSS[i].pditemid == id) {
        this.resultSS[i].result = result;
        break;
      }
    }
  }
}
